package com.tensquare.ssocleint.controller;

import com.tensquare.ssocleint.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/9/9-14:44
 * ------------------------------
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Value("${sso.server}")
    private String serverUrl;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 功能: 列表员工
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/9/9 14:44
     */
    @GetMapping("/list")
    public String list(Model model, HttpSession session,@RequestParam(required = false) String token){
        String url = serverUrl;
        //第一部分：根据token得到用户名
        if(!StringUtils.isEmpty(token)) {
            //1. 向服务端发请求根据token得到用户名
            ResponseEntity<String> entity = restTemplate.getForEntity(serverUrl + "getUsername?token=" + token, String.class);
            //2. 得到用户名
            String username = entity.getBody();
            //3. 如果存在用户名，就放到session中
            if(!StringUtils.isEmpty(username)){
                session.setAttribute("username",username);
            }
        }
        //第二部分：根据session决定跳转
        //1. 从session中得到用户名
        String username = (String) session.getAttribute("username");
        //2. 判断是否存在，如果不存在就去ssoserver.com登录
        if(StringUtils.isEmpty(username)){      //到单点登录服务器进行登录
            String serverUrl = url +  "login.html?redirect_uri=http://client1.com:9001/employee/list";
            return "redirect:" + serverUrl;
        }else{
            //1. 得到员工集合
            List<Employee> employeeList = initEmps();
            //2. 将员工列表放到model中
            model.addAttribute("emps",employeeList);
        }

        //第三部分：如果session与token都存在，就跳转到员工列表页面
        // if(!StringUtils.isEmpty(token)){
        //
        // }
        return "emps";
    }

    private List<Employee> initEmps() {
        //写法一：
        // return new ArrayList<Employee>(){{
        //     add(new Employee(1001,"张三","男",20,"上海"));
        //     add(new Employee(1002,"李四","男",22,"杭州"));
        //     add(new Employee(1003,"王五","女",21,"深圳"));
        // }};
        //写法二：
        return Arrays.asList(
                new Employee(1001,"张三","男",20,"上海"),
                new Employee(1002,"李四","男",22,"杭州"),
                new Employee(1003,"王五","女",21,"深圳"));
    }
}
